1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.base.Preconditions;
23  
24  import java.util.List;
25  
26  import javax.annotation.Nullable;
27  
28  /**
29   * Implementation of {@link ImmutableList} with exactly one element.
30   *
31   * @author Hayward Chan
32   */
33  @GwtCompatible(serializable = true, emulated = true)
34  @SuppressWarnings("serial") // uses writeReplace(), not default serialization
35  final class SingletonImmutableList<E> extends ImmutableList<E> {
36  
37    final transient E element;
38  
39    SingletonImmutableList(E element) {
40      this.element = checkNotNull(element);
41    }
42  
43    @Override
44    public E get(int index) {
45      Preconditions.checkElementIndex(index, 1);
46      return element;
47    }
48  
49    @Override public int indexOf(@Nullable Object object) {
50      return element.equals(object) ? 0 : -1;
51    }
52  
53    @Override public UnmodifiableIterator<E> iterator() {
54      return Iterators.singletonIterator(element);
55    }
56  
57    @Override public int lastIndexOf(@Nullable Object object) {
58      return indexOf(object);
59    }
60  
61    @Override
62    public int size() {
63      return 1;
64    }
65  
66    @Override public ImmutableList<E> subList(int fromIndex, int toIndex) {
67      Preconditions.checkPositionIndexes(fromIndex, toIndex, 1);
68      return (fromIndex == toIndex) ? ImmutableList.<E>of() : this;
69    }
70  
71    @Override public ImmutableList<E> reverse() {
72      return this;
73    }
74  
75    @Override public boolean contains(@Nullable Object object) {
76      return element.equals(object);
77    }
78  
79    @Override public boolean equals(@Nullable Object object) {
80      if (object == this) {
81        return true;
82      }
83      if (object instanceof List) {
84        List<?> that = (List<?>) object;
85        return that.size() == 1 && element.equals(that.get(0));
86      }
87      return false;
88    }
89  
90    @Override public int hashCode() {
91      // not caching hash code since it could change if the element is mutable
92      // in a way that modifies its hash code.
93      return 31 + element.hashCode();
94    }
95  
96    @Override public String toString() {
97      String elementToString = element.toString();
98      return new StringBuilder(elementToString.length() + 2)
99          .append('[')
100         .append(elementToString)
101         .append(']')
102         .toString();
103   }
104 
105   @Override public boolean isEmpty() {
106     return false;
107   }
108 
109   @Override boolean isPartialView() {
110     return false;
111   }
112 
113   @Override
114   int copyIntoArray(Object[] dst, int offset) {
115     dst[offset] = element;
116     return offset + 1;
117   }
118 }